package com.chima.action;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.chima.domain.Department;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.HsCheckDepartment;
import com.chima.service.DepartmentService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import flexjson.JSONSerializer;

public class DepartmentAction extends ActionSupport implements ModelDriven<Department>{
    Department department= new Department();
	public Department getModel() {	
		return department;
	}
	
	private  String searchdate1;
	public String getSearchdate1() {
		return searchdate1;
	}

	public void setSearchdate1(String searchdate1) {
		this.searchdate1 = searchdate1;
	}

	private int start;// 开始页码
	private int limit; //每页显示
	private int page;//页数
	private boolean success=false ;
	private DepartmentService departmentService;
	private List<Department> departments;

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String  ListDepartment() throws Exception{
	    PageRequestBean pageRequestBean = new PageRequestBean();
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean = departmentService.findDepartment(pageRequestBean);
		departments=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.include("*.id","*.name","*.xzorzy");
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.serialize(departments);  
        String resultJson2 =jsonSerializer.serialize(total);
        System.out.println(resultJson2);
        String jsonString = "{total:"+resultJson2.toString()+",departments:"+resultJson1.toString()+"}";
        System.out.print(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	
	  
  }
	public void addDepartment() throws Exception{
		try {
			departmentService.saveDepartment(department);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	
	public void editDepartment() throws Exception{
	departmentService.updateDepartment(department);
	this.success=true;
	JSONSerializer jsonSerializer = new JSONSerializer();
	String resultJson =jsonSerializer.serialize(success);
	String jsonString = "{success:"+resultJson.toString()+"}";
	System.out.println(jsonString);
	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
	ServletActionContext.getResponse().getWriter().print(jsonString);
}
	public String delete(){
			departmentService.deleteDepartment(department.getId());
		return NONE;
	}
	public String  ListDepartmentByflag() throws Exception{
		departments=departmentService.findByflag(1);
		// 输出json内容到客户端
	    JSONSerializer jsonSerializer = new JSONSerializer();
	    jsonSerializer.include("*.id","*.name");
	    jsonSerializer.exclude("*.class");
	    String resultJson1 = jsonSerializer.serialize(departments);  
	    String jsonString = "{departments:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
	public String  ListmubiaoDepartmentByflag() throws Exception{
		departments=departmentService.findByflag(0);
		// 输出json内容到客户端
	    JSONSerializer jsonSerializer = new JSONSerializer();
	    jsonSerializer.include("*.id","*.name");
	    jsonSerializer.exclude("*.class");
	    String resultJson1 = jsonSerializer.serialize(departments);  
	    String jsonString = "{departments:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
	public String  ListEfficiencyDepart() throws Exception{
		departments=departmentService.findByflag(2);
		// 输出json内容到客户端
	    JSONSerializer jsonSerializer = new JSONSerializer();
	    jsonSerializer.include("*.id","*.name");
	    jsonSerializer.exclude("*.class");
	    String resultJson1 = jsonSerializer.serialize(departments);  
	    String jsonString = "{departments:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
	public String  ListAllDepartment() throws Exception{
		departments=departmentService.findAll();
		// 输出json内容到客户端
	    JSONSerializer jsonSerializer = new JSONSerializer();
	    jsonSerializer.include("*.id","*.name");
	    jsonSerializer.exclude("*.class");
	    String resultJson1 = jsonSerializer.serialize(departments);  
	    String jsonString = "{departments:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
	public String  ListhscheckDepartment() throws Exception{
		System.out.println(searchdate1+"**********************************");
		List<HsCheckDepartment> hsCheckDepartments=new ArrayList<HsCheckDepartment>();
	    hsCheckDepartments=departmentService.findHsCheckDepartmentBymonth(searchdate1);
		// 输出json内容到客户端
	    JSONSerializer jsonSerializer = new JSONSerializer();
	    jsonSerializer.include("*.id","*.deptname","*.monthdate","*.flag","*.deptid");
	    jsonSerializer.exclude("*.class");
	    String resultJson1 = jsonSerializer.serialize(hsCheckDepartments);  
	    String jsonString = "{departments:"+resultJson1.toString()+"}";
	    System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	  
	}
	public String  trunhscheckDepartment() throws Exception{
		String checkdeptid=ServletActionContext.getRequest().getParameter("checkdeptid");
		HsCheckDepartment  hsCheckDepartment=departmentService.findhsCheckDepartmentByid(Integer.parseInt(checkdeptid));
		if (hsCheckDepartment.getFlag()==0) {
			hsCheckDepartment.setFlag(1);
		}else {
			hsCheckDepartment.setFlag(0);
		}
		
		departmentService.addhsCheckDepartment(hsCheckDepartment);
		success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
	public String  ListAllzhuyuanDepartment() throws Exception{
		departments=departmentService.findListAllzhuyuanDepartment();
		// 输出json内容到客户端
	    JSONSerializer jsonSerializer = new JSONSerializer();
	    jsonSerializer.include("*.id","*.name");
	    jsonSerializer.exclude("*.class");
	    String resultJson1 = jsonSerializer.serialize(departments);  
	    String jsonString = "{departments:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
}
